/**
 * 9:00 -- 18:00
 * 9:00 -- 12:00 工作时间
 * 12： 00 -- 13: 00 休息时间
 * 13:00 -- 18:00 工作时间
 *
 */

// 计算两个日期相差的工作小时（不包括节假日）
export const getHours= function (startTime, endTime) {
  startTime = new Date(startTime.replace(/-/g, '/'))
  endTime = new Date(endTime.replace(/-/g, '/'))
  const ms = Math.abs(endTime.getTime() - startTime.getTime())
  // 判断 起始 时间  星期几 （0，1，2，3，4，5，6）
  if ((startTime.getMonth() == endTime.getMonth())
    && (startTime.getDate() == endTime.getDate())) { // 若为同一日期
    if (startTime.getDay() === 0 || startTime.getDay() === 6) {
      return 0
    }
    const hour1 = (getDayHour(startTime.getHours(), startTime.getMinutes(),
      endTime.getHours(), endTime.getMinutes(), "1"))
    return hour1
  } else {
    var sh=startTime.getHours();
    var eh= endTime.getHours();
    var time1 = format(startTime)
    var time2 = format(endTime)
    var divdDays = getTime2Time(time1, time2)
    console.log('time12', time1, time2, divdDays)
    var workDayVal = getWorkDays(time1, time2, divdDays)
    console.log('workDayVal', workDayVal)
    // 起 休息
    if (startTime.getDay() === 0 || startTime.getDay() === 6) {
      if (endTime.getDay() === 0 || endTime.getDay() === 6) {
        return workDayVal * 8
      } else {
        var hour11 = workDayVal * 8 + getExpDayHour(endTime.getHours(), endTime.getMinutes())
        return hour11
      }
    } else {
      if (endTime.getDay() === 0 || endTime.getDay() === 6) {
        var hour11 = workDayVal * 8 + getExpDayHour(startTime.getHours(), startTime.getMinutes())
        return hour11
      } else {
        var hour0 = (workDayVal * 8 + getDayHour(startTime.getHours(),
          startTime.getMinutes(), endTime.getHours(), endTime
            .getMinutes(), "0"))
        return hour0;
      }
    }
  }
}

// 不在同一天
function getExpDayHour (h, m, type) {
  console.log(h, m, type)
  if (type == 'start') {
    if (m == 0) {
      if (9 <= h && h <= 12) {
        h = 12 - h + 5;
      } else if (13 <= h && h <= 18) {
        h = 18 - h;
      }
    } else {
      if (9 <= h && h <= 12) {
        h = 12 - h - 1 + (60 - m) / 60 + 5;
      } else if (13 <= h && h <= 18) {
        h = 18 - h - 1 + (60 - m) / 60
      }
    }
  } else {
    if (m == 0) {
      if (9 <= h && h <= 12) {
        h = h - 9
      } else if (13 <= h && h <= 18) {
        h = h - 13 + 3;
      }
    } else {
      if (9 <= h && h <= 12) {
        h = h - 9 + m / 60
      } else if (13 <= h && h <= 18) {
        h = 3 + h - 13 + m /60
      }
    }
  }

  return h
}
function getDayHour(sh, sm, eh, em, type) {
  sh = parseInt(sh);
  eh = parseInt(eh);
  if (sh < 9) {
    sh = 9
    sm = 0
  }
  if (eh > 18) {
    eh = 18
    em = 0
  }
  if (sh == 12 ) {
    sh = 13
    sm = 0
  }
  if (eh == 12) {
    sh = 13
    sm = 0
  }
  if (type == '0') {
    // 计算非当天
    sh = getExpDayHour(sh, sm, 'start')
    console.log('sh', sh)
    eh = getExpDayHour(eh, em, 'end')
    console.log('eh', eh)

    return parseFloat(sh + eh);
  } else {
    // 计算当天
    if (sh == eh) {
      // 在同一小时
      if (9 <= sh && sh <= 12 || 13 <= sh && sh <= 18) {
        return Math.abs((sm - em) / 60);
      } else {
        return 0
      }
    } else {
      // 不在同一小时
      // 开始时间在上午时间段，并且结束时间在下午时间段
      if ((9 <= sh && sh <= 12) && (13 <= eh && eh <= 18)) {
        if (eh == 18) {
          const remainder = ((12 - sh - 1) + (60-sm) / 60) +  ((eh - 13))

          return remainder
        } else {
          const remainder = ((12 - sh - 1) + (60 - sm) / 60) +  (eh - 13 + em / 60)

          return remainder
        }
        // 开始时间与结束时间都在上午时间段
      } else if ((9 <= sh && sh <= 12) && (9 <= eh && eh <= 12)) {
        if (sm == em) {
          return parseFloat(Math.abs(eh - sh));
        } else {

          return parseFloat(Math.abs(eh - sh - 1) + (60 - sm) / 60
            + em / 60);
        }

      } else if (13 <= sh && sh <= 18 && 13 <= eh && eh <= 18) {
        if (eh == 18) {
          if (sm == 0) {
            return parseFloat(Math.abs(eh - sh));
          } else {
            return parseFloat(Math.abs(eh - sh - 1) + (60 - sm) / 60
              + em / 60);
          }
        } else {
          if (sm == em) {
            return parseFloat(Math.abs(eh - sh));
          } else {
            return parseFloat(Math.abs(eh - sh - 1) + (60 - sm) / 60
              + em / 60);
          }
        }
      } else {
        return 0
      }
    }
  }
}

function format (time) {
  var y = time.getFullYear()
  var mon = time.getMonth() + 1
  var d = time.getDate()
  var h = time.getHours()
  var mm = time.getMinutes()
  var s= '00'
  mon = mon > 9 ? mon : '0' + mon
  d = d > 9 ? d : '0' + d
  h = h > 9 ? h : '0' + h
  mm = mm > 9 ? mm : '0' + mm
  // console.log (y + '-' + mon + '-' + d)
  return  (y + '-' + mon + '-' + d)
}
// 相隔天数
function getTime2Time(time1, time2)
{

  time1 = Date.parse(new Date(time1))/1000;
  time2 = Date.parse(new Date(time2))/1000;
  var time_ = time2 - time1;
  return (time_/(3600*24));
}

function getWorkDays (time1, time2, divDays) {
  var t1_day = new Date(time1).getDay()
  // console.log('t1_day', t1_day)
  // console.log('t2_day', t2_day)
  var t2_day = new Date(time2).getDay()
  // 0 1 2 3 4 5 6
  var temp = t1_day
  var days = 0
  for (let i = 0; i < divDays; i++) {
    // console.log('temp', temp)
    // console.log('days', days)
    if (temp != 0 && temp != 6) {
      if (i != 0) {
        days += 1
      }
    }
    temp++
    if (temp == 7) {
      temp = 0
    }
  }
  return days
}